﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class RFInventoryMgtBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IInventoryMgtDA InventoryMgtDA = DataAccess.CreateInventoryMgtDA();
        private static readonly ILocationDA locationDA = DataAccess.CreateLocationDA();
        private PubParasInfo pubParasInfo;
        private string whLoginID;

        public RFInventoryMgtBLL()
        {
            this.whLoginID = string.Empty;
            this.pubParasInfo = null;
        }

        public RFInventoryMgtBLL(string currentWH)
        {
            this.whLoginID = string.Empty;
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
                this.whLoginID = this.dataBaseID[0];
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public RFInventoryMgtBLL(PubParasInfo tempPubParasInfo)
        {
            this.whLoginID = string.Empty;
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                    this.whLoginID = this.dataBaseID[0];
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string ExecInvMoveByLoc(string fromLoc, string toLoc)
        {
            string result = string.Empty;
            if (!string.IsNullOrEmpty(fromLoc) && !string.IsNullOrEmpty(toLoc))
            {
                if (!locationDA.CheckLocationIDUnique(toLoc, this.whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F5050_001", this.pubParasInfo.UserLanguage);
                }
                InventoryMgtBLL inventoryMgtBLL = new InventoryMgtBLL(this.pubParasInfo);
                List<InventoryInfo> inventoryInfoList = InventoryMgtDA.GetInventoryInfoByLoc(fromLoc, this.whLoginID);
                if ((inventoryInfoList == null) || (inventoryInfoList.Count <= 0))
                {
                    return result;
                }
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    for (int i = 0; i < inventoryInfoList.Count; i++)
                    {
                        InvMovesInfo invMovesInfo = new InvMovesInfo {
                            DocID = "",
                            DocLineID = 0,
                            FromLoc = inventoryInfoList[i].Loc,
                            FromLot = inventoryInfoList[i].Lot,
                            FromLpn = inventoryInfoList[i].LpnID,
                            FromMoveQty = ((((inventoryInfoList[i].Qty - inventoryInfoList[i].AllocatedQty) - inventoryInfoList[i].ArchiveQty) - inventoryInfoList[i].ExpectedQty) - inventoryInfoList[i].PendingMoveIn) - inventoryInfoList[i].PickedQty,
                            FromOwnerID = inventoryInfoList[i].OwnerID,
                            FromSkuID = inventoryInfoList[i].SkuID,
                            MoveQty = ((((inventoryInfoList[i].Qty - inventoryInfoList[i].AllocatedQty) - inventoryInfoList[i].ArchiveQty) - inventoryInfoList[i].ExpectedQty) - inventoryInfoList[i].PendingMoveIn) - inventoryInfoList[i].PickedQty,
                            ToLoc = toLoc,
                            ToLot = inventoryInfoList[i].Lot,
                            ToLpn = inventoryInfoList[i].LpnID,
                            ToOwnerID = inventoryInfoList[i].OwnerID,
                            ToSkuID = inventoryInfoList[i].SkuID,
                            UpdatedBy = this.pubParasInfo.UserID,
                            FromPackID = "",
                            FromUomID = "",
                            PackID = "",
                            UomID = ""
                        };
                        inventoryMgtBLL.UpdateInvLotLocLpn4Moves(dataBase, tran, invMovesInfo, InvMoveType.Move, this.whLoginID);
                        inventoryMgtBLL.UpdateInvSkuLoc4Moves(dataBase, tran, invMovesInfo, InvMoveType.Move, this.whLoginID);
                        inventoryMgtBLL.InsertInvMovesTrans(dataBase, tran, invMovesInfo, TransSourceType.ConsoleMove, this.whLoginID);
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    result = ex.Message;
                    CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                }
                finally
                {
                    conn.Close();
                }
            }
            return result;
        }

        public string ExecInvMoveByLpnList(List<string> lpnList, string moveToLoc)
        {
            string result = string.Empty;
            if ((lpnList != null) && (lpnList.Count > 0))
            {
                if (!locationDA.CheckLocationIDUnique(moveToLoc, this.whLoginID))
                {
                    return PublicMethod.GetInstance().RF_ShowAlertMessage("货位无效; 貨位無效; Stage is invalid", this.pubParasInfo.UserLanguage);
                }
                InventoryMgtBLL inventoryMgtBLL = new InventoryMgtBLL(this.pubParasInfo);
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                InventoryInfo inventoryInfo = null;
                try
                {
                    for (int i = 0; i < lpnList.Count; i++)
                    {
                        inventoryInfo = InventoryMgtDA.GetInventoryInfoByLpnID(dataBase, tran, lpnList[i], this.whLoginID);
                        if (inventoryInfo != null)
                        {
                            InvMovesInfo invMovesInfo = new InvMovesInfo {
                                DocID = "",
                                DocLineID = 0,
                                FromLoc = inventoryInfo.Loc,
                                FromLot = inventoryInfo.Lot,
                                FromLpn = inventoryInfo.LpnID,
                                FromMoveQty = ((((inventoryInfo.Qty - inventoryInfo.AllocatedQty) - inventoryInfo.ArchiveQty) - inventoryInfo.ExpectedQty) - inventoryInfo.PendingMoveIn) - inventoryInfo.PickedQty,
                                FromOwnerID = inventoryInfo.OwnerID,
                                FromSkuID = inventoryInfo.SkuID,
                                MoveQty = ((((inventoryInfo.Qty - inventoryInfo.AllocatedQty) - inventoryInfo.ArchiveQty) - inventoryInfo.ExpectedQty) - inventoryInfo.PendingMoveIn) - inventoryInfo.PickedQty,
                                ToLoc = moveToLoc,
                                ToLot = inventoryInfo.Lot,
                                ToLpn = inventoryInfo.LpnID,
                                ToOwnerID = inventoryInfo.OwnerID,
                                ToSkuID = inventoryInfo.SkuID,
                                UpdatedBy = this.pubParasInfo.UserID,
                                FromPackID = "",
                                FromUomID = "",
                                PackID = "",
                                UomID = ""
                            };
                            inventoryMgtBLL.UpdateInvLotLocLpn4Moves(dataBase, tran, invMovesInfo, InvMoveType.Move, this.whLoginID);
                            inventoryMgtBLL.UpdateInvSkuLoc4Moves(dataBase, tran, invMovesInfo, InvMoveType.Move, this.whLoginID);
                            inventoryMgtBLL.InsertInvMovesTrans(dataBase, tran, invMovesInfo, TransSourceType.ConsoleMove, this.whLoginID);
                        }
                        else
                        {
                            result = result + lpnList[i] + ",";
                        }
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                }
                finally
                {
                    conn.Close();
                }
            }
            return (string.IsNullOrEmpty(result) ? "" : ("LPN: " + result.Substring(0, result.Length - 1) + PublicMethod.GetInstance().RF_ShowAlertMessage(" 没有对应的库存记录; 沒有對應的庫存記錄; have not inventory record", this.pubParasInfo.UserLanguage)));
        }

        public List<InventoryInfo> GetInventoryListInfoByLpnID(string type, string lpnID)
        {
            string whLoginID = this.dataBaseID[0];
            string where = " AND ILLL.LPN_ID = '" + lpnID + "'";
            return InventoryMgtDA.GetInventoryInfoByWhere(where, whLoginID);
        }
    }
}

